//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.tool.custom.imageview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

@SuppressLint({"HandlerLeak"})
public class CWin8ImageView extends ImageView {
    public static final int Rotate_Handler_Message_Start = 1;
    public static final int Rotate_Handler_Message_Turning = 2;
    public static final int Rotate_Handler_Message_Turned = 3;
    public static final int Rotate_Handler_Message_Reverse = 6;
    public static final int Scale_Handler_Message_Start = 1;
    public static final int Scale_Handler_Message_Turning = 2;
    public static final int Scale_Handler_Message_Turned = 3;
    public static final int Scale_Handler_Message_Reverse = 6;
    private boolean isAntiAlias = true;
    private boolean scaleOnly = false;
    private boolean isSizeChanged = false;
    private boolean isShowAnimation = true;
    private int rotateDegree = 10;
    private boolean isFirst = true;
    private float minScale = 0.95F;
    private int vWidth;
    private int vHeight;
    private boolean isAnimationFinish = true;
    private boolean isActionMove = false;
    private boolean isScale = false;
    private Camera camera = new Camera();
    boolean XbigY = false;
    float RolateX = 0.0F;
    float RolateY = 0.0F;
    CWin8ImageView.OnViewClick onclick = null;
    @SuppressLint({"HandlerLeak"})
    private Handler rotate_Handler = new Handler() {
        private Matrix matrix = new Matrix();
        private float count = 0.0F;

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            this.matrix.set(CWin8ImageView.this.getImageMatrix());
            switch(msg.what) {
                case 1:
                    this.count = 0.0F;
                    CWin8ImageView.this.beginRotate(this.matrix, CWin8ImageView.this.XbigY?this.count:0.0F, CWin8ImageView.this.XbigY?0.0F:this.count);
                    CWin8ImageView.this.rotate_Handler.sendEmptyMessage(2);
                    break;
                case 2:
                    CWin8ImageView.this.beginRotate(this.matrix, CWin8ImageView.this.XbigY?this.count:0.0F, CWin8ImageView.this.XbigY?0.0F:this.count);
                    ++this.count;
                    if(this.count < (float)CWin8ImageView.this.getDegree()) {
                        CWin8ImageView.this.rotate_Handler.sendEmptyMessage(2);
                    } else {
                        CWin8ImageView.this.isAnimationFinish = true;
                    }
                    break;
                case 3:
                    CWin8ImageView.this.beginRotate(this.matrix, CWin8ImageView.this.XbigY?this.count:0.0F, CWin8ImageView.this.XbigY?0.0F:this.count);
                    if(this.count > 0.0F) {
                        CWin8ImageView.this.rotate_Handler.sendEmptyMessage(3);
                    } else {
                        CWin8ImageView.this.isAnimationFinish = true;
                        if(!CWin8ImageView.this.isActionMove && CWin8ImageView.this.onclick != null) {
                            CWin8ImageView.this.onclick.onClick();
                        }
                    }

                    --this.count;
                    --this.count;
                case 4:
                case 5:
                default:
                    break;
                case 6:
                    this.count = (float)CWin8ImageView.this.getDegree();
                    CWin8ImageView.this.beginRotate(this.matrix, CWin8ImageView.this.XbigY?this.count:0.0F, CWin8ImageView.this.XbigY?0.0F:this.count);
                    CWin8ImageView.this.rotate_Handler.sendEmptyMessage(3);
            }

        }
    };
    private Handler scale_handler = new Handler() {
        private Matrix matrix = new Matrix();
        private float s;
        int count = 0;

        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            this.matrix.set(CWin8ImageView.this.getImageMatrix());
            switch(msg.what) {
                case 1:
                    if(!CWin8ImageView.this.isAnimationFinish) {
                        return;
                    }

                    CWin8ImageView.this.isAnimationFinish = false;
                    CWin8ImageView.this.isSizeChanged = true;
                    this.count = 0;
                    this.s = (float)Math.sqrt(Math.sqrt((double)CWin8ImageView.this.minScale));
                    CWin8ImageView.this.beginScale(this.matrix, this.s);
                    CWin8ImageView.this.scale_handler.sendEmptyMessage(2);
                    break;
                case 2:
                    CWin8ImageView.this.beginScale(this.matrix, this.s);
                    if(this.count < 4) {
                        CWin8ImageView.this.scale_handler.sendEmptyMessage(2);
                    } else {
                        CWin8ImageView.this.isAnimationFinish = true;
                        if(!CWin8ImageView.this.isSizeChanged && !CWin8ImageView.this.isActionMove && CWin8ImageView.this.onclick != null) {
                            CWin8ImageView.this.onclick.onClick();
                        }
                    }

                    ++this.count;
                case 3:
                case 4:
                case 5:
                default:
                    break;
                case 6:
                    if(!CWin8ImageView.this.isAnimationFinish) {
                        CWin8ImageView.this.scale_handler.sendEmptyMessage(6);
                    } else {
                        CWin8ImageView.this.isAnimationFinish = false;
                        this.count = 0;
                        this.s = (float)Math.sqrt(Math.sqrt((double)(1.0F / CWin8ImageView.this.minScale)));
                        CWin8ImageView.this.beginScale(this.matrix, this.s);
                        CWin8ImageView.this.scale_handler.sendEmptyMessage(2);
                        CWin8ImageView.this.isSizeChanged = false;
                    }
            }

        }
    };

    public CWin8ImageView(Context context) {
        super(context);
    }

    public CWin8ImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void SetAnimationOnOff(boolean oo) {
        this.isShowAnimation = oo;
    }

    public void setOnClickIntent(CWin8ImageView.OnViewClick onclick) {
        this.onclick = onclick;
    }

    @SuppressLint({"DrawAllocation"})
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(this.isFirst) {
            this.isFirst = false;
            this.init();
        }

        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, 3));
    }

    public void init() {
        this.vWidth = this.getWidth() - this.getPaddingLeft() - this.getPaddingRight();
        this.vHeight = this.getHeight() - this.getPaddingTop() - this.getPaddingBottom();
        Drawable drawable = this.getDrawable();
        if(drawable != null) {
            BitmapDrawable bd = (BitmapDrawable)drawable;
            bd.setAntiAlias(this.isAntiAlias);
        }

    }

    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        if(!this.isShowAnimation) {
            return true;
        } else {
            switch(event.getAction() & 255) {
                case 0:
                    float X = event.getX();
                    float Y = event.getY();
                    this.RolateX = (float)(this.vWidth / 2) - X;
                    this.RolateY = (float)(this.vHeight / 2) - Y;
                    this.XbigY = Math.abs(this.RolateX) > Math.abs(this.RolateY);
                    this.isScale = X > (float)(this.vWidth / 3) && X < (float)(this.vWidth * 2 / 3) && Y > (float)(this.vHeight / 3) && Y < (float)(this.vHeight * 2 / 3);
                    this.isActionMove = false;
                    if(this.isScale) {
                        if(this.isAnimationFinish && !this.isSizeChanged) {
                            this.isSizeChanged = true;
                            this.scale_handler.sendEmptyMessage(1);
                        }
                    } else if(this.scaleOnly) {
                        this.scale_handler.sendEmptyMessage(1);
                    } else {
                        this.rotate_Handler.sendEmptyMessage(1);
                    }
                    break;
                case 1:
                    if(this.isScale) {
                        if(this.isSizeChanged) {
                            this.scale_handler.sendEmptyMessage(6);
                        }
                    } else {
                        this.rotate_Handler.sendEmptyMessage(6);
                    }
                    break;
                case 2:
                    float x = event.getX();
                    float y = event.getY();
                    if(x <= (float)this.vWidth && y <= (float)this.vHeight && x >= 0.0F && y >= 0.0F) {
                        this.isActionMove = false;
                    } else {
                        this.isActionMove = true;
                    }
            }

            return true;
        }
    }

    private synchronized void beginRotate(Matrix matrix, float rotateX, float rotateY) {
        int scaleX = (int)((float)this.vWidth * 0.5F);
        int scaleY = (int)((float)this.vHeight * 0.5F);
        this.camera.save();
        this.camera.rotateX(this.RolateY > 0.0F?rotateY:-rotateY);
        this.camera.rotateY(this.RolateX < 0.0F?rotateX:-rotateX);
        this.camera.getMatrix(matrix);
        this.camera.restore();
        if(this.RolateX > 0.0F && rotateX != 0.0F) {
            matrix.preTranslate((float)(-this.vWidth), (float)(-scaleY));
            matrix.postTranslate((float)this.vWidth, (float)scaleY);
        } else if(this.RolateY > 0.0F && rotateY != 0.0F) {
            matrix.preTranslate((float)(-scaleX), (float)(-this.vHeight));
            matrix.postTranslate((float)scaleX, (float)this.vHeight);
        } else if(this.RolateX < 0.0F && rotateX != 0.0F) {
            matrix.preTranslate(0.0F, (float)(-scaleY));
            matrix.postTranslate(0.0F, (float)scaleY);
        } else if(this.RolateY < 0.0F && rotateY != 0.0F) {
            matrix.preTranslate((float)(-scaleX), 0.0F);
            matrix.postTranslate((float)scaleX, 0.0F);
        }

        this.setImageMatrix(matrix);
    }

    private synchronized void beginScale(Matrix matrix, float scale) {
        int scaleX = (int)((float)this.vWidth * 0.5F);
        int scaleY = (int)((float)this.vHeight * 0.5F);
        matrix.postScale(scale, scale, (float)scaleX, (float)scaleY);
        this.setImageMatrix(matrix);
    }

    public int getDegree() {
        return this.rotateDegree;
    }

    public void setDegree(int degree) {
        this.rotateDegree = degree;
    }

    public float getScale() {
        return this.minScale;
    }

    public void setScale(float scale) {
        this.minScale = scale;
    }

    public interface OnViewClick {
        void onClick();
    }
}
